package com.zbkj.dal.pojo.response.ba;

import com.google.common.collect.Lists;
import com.zbkj.dal.base.enums.ParameterInputTypeEnum;
import com.zbkj.dal.entity.product.Parameter;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 最终输入给前端的完整对象
 * @author tianzhonghai
 * 2023/11/1 14:34
 */
@Data
public class ProductParameterCombineResponse {

    private Integer parameterId;

    private String name;

    /**
     * 类型：radio, input, checkbox
     */
    private String type;

    /**
     * 选项，以逗号分隔
     */
    private String options;

    private String value;


    private List<String> optionArr;

    private List<String> values;

    public static ProductParameterCombineResponse create(Parameter parameter) {
        return create(parameter, "");
    }

    public static ProductParameterCombineResponse create(Parameter parameter, String value) {
        ProductParameterCombineResponse productParameterCombineResponse = new ProductParameterCombineResponse();
        BeanUtils.copyProperties(parameter, productParameterCombineResponse);
        productParameterCombineResponse.setParameterId(parameter.getId());
        productParameterCombineResponse.setValue(value);
        if (ParameterInputTypeEnum.supportOption(productParameterCombineResponse.getType())) {
            if (StringUtils.isNotBlank(productParameterCombineResponse.getOptions())) {
                String[] opts = StringUtils.split(productParameterCombineResponse.getOptions(), ",");
                productParameterCombineResponse.setOptionArr(Arrays.stream(opts).collect(Collectors.toList()));
            }


            if (!ParameterInputTypeEnum.singleValue(productParameterCombineResponse.getType()) && StringUtils.isNotBlank(value)) {
                String[] opts = StringUtils.split(productParameterCombineResponse.getValue(), ",");
                productParameterCombineResponse.setValues(Arrays.stream(opts).collect(Collectors.toList()));
            }
        }

        if (productParameterCombineResponse.getValues() == null) {
            productParameterCombineResponse.setValues(Lists.newArrayList());
        }
        return productParameterCombineResponse;
    }
}
